<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
<!--      <div class="flex flex-center topbar">-->
<!--        <div class="flex-full inputbox flex flex-center block" @click="onSearchTap">-->
<!--          <img class="icon" mode="aspectFit" src="@/assets/img/ic_search.png"/>-->
<!--          <input disabled style="text-align: left" class="input flex-full" placeholder="搜索商品"/>-->
<!--        </div>-->
<!--      </div>-->

      <div class="toptab">
        <div
          v-for="(item,index) in tab"
          :key="index"
          class="item"
          :class="tabIndex==index?'active':''"
          @click="onTabTap(index)"
        >{{((item.discount_rate)/10)}}折专区
        </div>
      </div>
    </div>
      <div class="page">
        <div style="margin-top: 12px">
          <Swiper
            class="swiper"
            :showDescMask="false"
            dots-position="center"
            auto
            :height="'206px'"
          >
            <swiper-item @click.native="onInfo(item.adv_goods_skus)" class="swiper-demo-img" v-for="(item, index) in swipers" :key="index"><img style="width: 100%;height: 100%" :src="item.adv_img"></swiper-item>
          </Swiper>
        </div>
        <div class="block" v-if="items.length" style="margin-top: 24px">
          <!-- <div style="margin-bottom: 12px">{{tab[tabIndex]}}折专区</div> -->
          <div class="grid grid-gutter discountbox">
            <div class="grid-1-2" v-for="(item,index) in items" :key="index">
              <div class="item" @click="(onInfo(item.goods_info.goods_sku))" data-product="product">
                <chimage class="image" :src="item.goods_info.goods_image_main" mode="aspectFill"></chimage>
                <div class="title">{{item.goods_info.goods_name}}</div>
                <div class="price">
                  <div>折后价￥{{item.discount_price}}</div>
                  <div class="d-price">￥{{item.goods_info.reference_price}}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
  </div>
</template>

<script>
  import Product from '@/dll/product'

  export default {
    name: 'mallDiscount',
    data () {
      return {
        xheader: {
          title: '折扣'
        },
        tab: [],
        items: [],
        tabIndex: 0,
        swipers: []
      }
    },
    created () {
      this.getNewDiscountList()
    },
    methods: {
      onInfo (sku) {
        this.$router.push({ name: 'mallInfo', params: { id: sku } })
      },
      onTabTap (index, item) {
        this.tabIndex = index
        this.items = item.discount_goods_list
      },
      // 获取折扣列表
      async getNewDiscountList () {
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Product.getNewDiscountList()
        this.$vux.loading.hide()
        if (res.code == 0) {
          if (!res.data.discount_list.length) {
            this.$vux.confirm.show({
              title: '提示',
              content: '暂无相关活动，请稍后再来。',
              showCancelButton: false,
              onConfirm: () => {
                this.$router.back()
              }
            })
            return
          }
          this.tab = res.data.discount_list
          this.swipers = res.data.banner_info.list
          this.items = res.data.discount_list && res.data.discount_list[0].discount_goods_list
        } else {
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding-left: 12px;
    padding-right: 12px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

  .toptab {
    border-top: 1px solid #fbf8f7;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    padding-left: 12px;
    padding-right: 12px;
    height: 45px;
    z-index: 9999;
    overflow: auto;
    background-color: #fff;
  }

  .toptab > .item {
    min-width: 25%;
    height: 100%;
    display: block;
    line-height: 45px;
    font-size: 14px;
    color: #9f8d89;
    text-align: center;
    position: relative;
  }

  .toptab > .item.active {
    color: #fe0000;
  }

  .toptab > .item.active::after {
    position: absolute;
    content: "";
    width: 100%;
    bottom: 0;
    left: 0;
    height: 1px;
    background: #fe0000;
  }

  .swiper {
    height: 206px;
    background-color: #fff;
  }

  .slide-image {
    height: 206px;
    width: 100%;
  }

  .discountbox .item {
    display: block;
    background-color: #fff;
    padding: 12px;
    box-sizing: border-box;
  }

  .discountbox .item > .image {
    display: block;
    height: 145px;
    width: 100%;
    background-color: #fbf8f7;
    background-size: cover;
  }

  .discountbox .item > .title {
    margin-top: 16px;
    font-size: 12px;
    color: #575757;
    line-height: 17px;
    height: 34px;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .discountbox .item > .price {
    font-size: 14px;
    color: #fe0000;
    line-height: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 3px;
  }

  .discountbox .item > .price .d-price {
    font-size: 12px;
    color: #d3cbc9;
    line-height: 17px;
    text-decoration: line-through;
  }

  .page {
    padding-top: 45px;
  }

  .pagetop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
</style>
